package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dao.PostDao;
import com.example.dao.UserDao;
import com.example.domain.*;
import com.example.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@Controller
@RequestMapping("post")
public class PostController {
    @Autowired
    private PostService postService;

    @GetMapping("toPost")
    public String toSeek() {
        return "post/post";
    }

    @GetMapping("getPage")
    @ResponseBody
    public TableData getPage(Integer page, Integer limit, String username, String content) {
        return postService.getPage(page, limit, username, content);
    }

    @PostMapping("deletePost")
    @ResponseBody
    public DataJson deletePost(@RequestParam("id") Integer id) {
        postService.removeById(id);
        DataJson dataJson = new DataJson();
        dataJson.setCode(0);
        return dataJson;
    }

    @PostMapping("batchDeletePost")
    @ResponseBody
    public DataJson batchDeletePost(@RequestParam("ids[]") Integer ids[]) {

        postService.removeByIds(Arrays.asList(ids));
        DataJson dataJson = new DataJson();
        dataJson.setCode(0);
        dataJson.setMsg("删除成功");
        return dataJson;
    }

    @PostMapping("addPost")
    @ResponseBody
    public DataJson addPost(@RequestBody Post post) {
        postService.save(post);
        DataJson dataJson = new DataJson();
        dataJson.setCode(0);
        return dataJson;
    }

    @PostMapping("upPost")
    @ResponseBody
    public DataJson upPost(Integer id) {
        Post post = postService.getById(id);
        post.setStatus(0);
        postService.updateById(post);
        DataJson dataJson = new DataJson();
        dataJson.setCode(0);
        return dataJson;
    }

    @PostMapping("downPost")
    @ResponseBody
    public DataJson downPost(Integer id) {
        Post post = postService.getById(id);
        post.setStatus(1);
        postService.updateById(post);
        DataJson dataJson = new DataJson();
        dataJson.setCode(0);
        return dataJson;
    }


    //微信接口
    @Autowired
    private PostDao postDao;
    @Autowired
    private UserDao userDao;

    @GetMapping("getAllPost")
    @ResponseBody
    public Result getAllPost(Integer page, Integer limit) {
        Result result = new Result();

        IPage<Post> ipage = new Page(page, limit);

        LambdaQueryWrapper<Post> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(Post::getStatus, 0).orderByDesc(Post::getId);

        IPage<Post> postIPage = postDao.selectPage(ipage, lambdaQueryWrapper);

        System.out.println(postIPage.getRecords());
        for (Post post : postIPage.getRecords()) {

            User user = userDao.selectById(post.getUserId());

            post.setUsername(user.getUsername());

        }

        return Result.success(postIPage.getRecords());

    }


    @PostMapping("publishPost")
    @ResponseBody
    public Result publishPost(Integer userId, String content, Integer status) {
        Result result = new Result();
        Post post = new Post();
        post.setUserId(userId);
        post.setContent(content);
        post.setStatus(status);

        System.out.println(post);
        postService.save(post);
//        postDao.insert(post);

        return Result.success("发布成功");

    }


    @GetMapping("getPublishPost")
    @ResponseBody
    public Result getPublishPost(Integer userId, Integer page, Integer limit) {


        LambdaQueryWrapper<Post> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(userId != null, Post::getUserId, userId).orderByDesc(Post::getId);

        IPage<Post> iPage = new Page(page, limit);
        IPage<Post> postIPage = postDao.selectPage(iPage, lambdaQueryWrapper);
        for (Post post : postIPage.getRecords()) {
            if (post.getStatus() == 0) {
                post.setStatusName("正常");
            } else if (post.getStatus() == 1) {
                post.setStatusName("已被管理员下线");
            }
        }
        System.out.println("11111" + postIPage.getRecords());

        return Result.success(postIPage.getRecords());
    }

    @PostMapping("delete")
    @ResponseBody
    public Result delete(Integer id) {


        postService.removeById(id);

        return Result.success("删除成功");
    }
}
